package com.xiaodimall.beforesys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaodimall.common.Constants;
import com.xiaodimall.common.ServiceResultEnum;
import com.xiaodimall.beforesys.mapper.UserMapper;
import com.xiaodimall.beforesys.pojo.User;
import com.xiaodimall.beforesys.service.UserService;
import com.xiaodimall.utils.Email;
import com.xiaodimall.utils.JsonResult;
import com.xiaodimall.utils.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author wyc
 * @description
 * @ClassName UserServiceImpl
 * @date 2022/11/24 15:54
 */
@Service
public class UserServiceImpl implements UserService {


    //注入用户持久层接口
    @Resource
    private UserMapper userMapper;


    /**
     * 获取邮箱验证码
     * @author WYC
     * @param email
     * @param session
     * @return: 结果集
     * @date: 2022/11/24 16:35
     */
    @Override
    public JsonResult sendEmailService(String email, HttpSession session) {
        //
        if (StringUtils.isEmpty(email)){
            return new JsonResult(201,"邮箱不能为空");
        }
        String res = Email.sendEmail(email);
        if (res == null || "".equals(res)) {
            return new JsonResult(204, "邮箱验证码发送失败,请稍后重试");
        }
        //绑定验证码
        session.setAttribute("code", res);

        return new JsonResult(200, "发送成功");
    }


    /**
     * 验证注册功能
     * @author WYC
     * @param loginName
     * @param password
     * @param email
     * @return: 返回结果集
     * @date: 2022/11/24 16:29
     */
    @Override
    public String register(String loginName, String password,
                           String email,String verifyCode,
                           HttpSession httpSession) {
        //对参数进行判断
        if (StringUtils.isEmpty(loginName)) {
            return ServiceResultEnum.LOGIN_NAME_NULL.getResult();
        }
        if (StringUtils.isEmpty(password)) {
            return ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult();
        }
        if (StringUtils.isEmpty(verifyCode)) {
            return ServiceResultEnum.LOGIN_VERIFY_CODE_NULL.getResult();
        }
        //获取验证码
        Object code = httpSession.getAttribute("code");

        if (StringUtils.isEmpty(code) || !verifyCode.toLowerCase().equals(code)) {
            return ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult();
        }
        //查询有没有用户名是否重复
        User user = userMapper.selectByLoginName(loginName);
        if ( user!= null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        //添加数据注册信息到数据库
        User registerUser = new User();
        registerUser.setLoginName(loginName);
        registerUser.setNickName(loginName);
        registerUser.setEmail(email);
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        registerUser.setPasswordMd5(passwordMD5);
        //调用持久层方法将数据添加到数据库
        int row = userMapper.insert(registerUser);
        //成功与否
        if ( row > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }


    /**
     * 登录功能的实现
     * @author WYC
     * @param loginName
     * @param password
     * @param httpSession
     * @return: 返回结果集
     * @date: 2022/11/24 20:28
     */
    @Override
    public String login(String loginName, String password,
                        String verifyCode, HttpSession httpSession) {
        //对参数进行判断
        if (StringUtils.isEmpty(loginName)) {
            return ServiceResultEnum.LOGIN_NAME_NULL.getResult();
        }
        if (StringUtils.isEmpty(password)) {
            return ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult();
        }
        if (StringUtils.isEmpty(verifyCode)) {
            return ServiceResultEnum.LOGIN_VERIFY_CODE_NULL.getResult();
        }
        //判断验证码
        Object code = httpSession.getAttribute(Constants.MALL_VERIFY_CODE_KEY);
        if (StringUtils.isEmpty(code) || !verifyCode.toLowerCase().equals(code)) {
            return ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult();
        }
        //把密码加密成md5值
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        //调用持久层方法
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("login_name",loginName));
        //判断密码和用户名是否正确
        if (user.getLoginName()==null||!passwordMD5.equals(user.getPasswordMd5())) {
            return ServiceResultEnum.LOGIN_LOGIN_USER_ADN_PASSWORD_ERROR.getResult();
        }
        //判断用户的状态
        if (user != null && httpSession != null) {
            if (user.getLockedFlag() == 1) {

                return ServiceResultEnum.LOGIN_USER_LOCKED.getResult();
            }
            //昵称太长影响页面展示,所以支取前7位
            if (user.getNickName() != null && user.getNickName().length() > 7) {
                String tempNickName = user.getNickName().substring(0, 7) + "..";
                user.setNickName(tempNickName);
            }
            //绑定数据
            httpSession.setAttribute(Constants.MALL_USER_SESSION_KEY,user);
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }



    /**
     * 修改用户信息和地址
     * @author WYC
     * @param mallUser
     * @param httpSession
     * @return: 返回修改后的信息
     * @date: 2022/11/30 18:07
     */
    @Override
    public User updateUserInfo(User mallUser, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        //查询出原本的用户信息
        User userInfo = userMapper.selectOne(new QueryWrapper<User>()
                .eq("user_id", user.getUserId())
                .eq("is_deleted", 0));
        if (userInfo != null) {
            //设置名称
            if (!StringUtils.isEmpty(mallUser.getNickName())){
                userInfo.setNickName(mallUser.getNickName());
            }
            //设置地址
            if (!StringUtils.isEmpty(mallUser.getAddress())){
                userInfo.setAddress(mallUser.getAddress());
            }
            //设置个签
            if (!StringUtils.isEmpty(mallUser.getIntroduceSign())){
                userInfo.setIntroduceSign(mallUser.getIntroduceSign());
            }
            //执行修改操作
            int row = userMapper.update(userInfo, new QueryWrapper<User>()
                    .eq("user_id", user.getUserId())
                    .eq("is_deleted", 0));
            if (row>0) {
                //查询用户数据并重新绑定数据
                User user1 = userMapper.selectById(user.getUserId());
                httpSession.setAttribute(Constants.MALL_USER_SESSION_KEY,user1);
                return user1;

            }
        }
            return null;
        }
    }

